package com.winit.openapi.wh.outbound.builder;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.erp.spi.order.vo.OutboundOrderWorkOrderHelpVo2;
import com.winit.oms.spi.outbound.command.order.QueryOrderBySellerCommand;
import com.winit.oms.spi.outbound.vo.OutboundOrderAddressCheckTheResultVo;
import com.winit.oms.spi.outbound.vo.OutboundOrderBillingVo;
import com.winit.oms.spi.outbound.vo.OutboundOrderEnumContants;
import com.winit.oms.spi.outbound.vo.OutboundOrderEnumContants.OrderSource;
import com.winit.oms.spi.outbound.vo.OutboundOrderEnumContants.OrderType;
import com.winit.oms.spi.outbound.vo.OutboundOrderVo;
import com.winit.oms.spi.outbound.vo.OutboundOrderWorkOrderHelpVo;
import com.winit.oms.spi.outbound.vo.OutboundOrderWorkOrderMerchandiseHelpVo;
import com.winit.oms.spi.outbound.vo.OutboundOrderWorkOrderVo;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderMerchandiseVo;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderRebateVo;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderRevenueVo;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderSlaDetailVo;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderSlaVo;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderValueAddedServiceVo;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderVasAttributeVo;
import com.winit.oms.spi.v2.tools.request.ProductRecommendRequestParams;
import com.winit.oms.spi.v2.tools.request.RequestParamsMerchandises;
import com.winit.oms.spi.v2.tools.request.RequestParamsParcels;
import com.winit.oms.spi.v2.tools.request.RequestParamsReceiveAddress;
import com.winit.oms.spi.v2.tools.request.RequestParamsVAS;
import com.winit.oms.spi.v2.tools.request.RequestParamsVASAttrs;
import com.winit.oms.spi.vo.EbayOrderVo;
import com.winit.oms.spi.vo.v2.order.BuyerInfo;
import com.winit.oms.spi.vo.v2.order.CustomerInfo;
import com.winit.oms.spi.vo.v2.order.ProductInfo;
import com.winit.oms.spi.vo.v2.order.WarehouseInfo;
import com.winit.oms.spi.vo.v2.response.ErrorResult;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.util.DateUtil;
import com.winit.openapi.wh.outbound.validator.OutboundOrderfieldValidator;
import com.winit.openapi.wh.utils.JsonValid;

public class OutboundOrderBuilder {

    /**
     * 批量导入订单构建
     * 
     * @param jsons
     * @return
     */
    public static OutboundOrderVo build(JSONObject jsons) {
        JsonValid json = new JsonValid(jsons);

        OutboundOrderVo target = new OutboundOrderVo();
        target.setOrderSource(OrderSource.MI.getSource());
        target.setStoreType(json.getString("storeType")); // 平台名称
        target.setSaleType(OutboundOrderEnumContants.saleType.SI.getStatus());
        // target.setBulkType(OutboundOrderEnumContants.bulkType.NLM.getStatus());
        // 产品信息
        ProductInfo productInfo = new ProductInfo();
        productInfo.setWinitProductCategory3(json.getString("orderType"));
        productInfo.setWinitProductCode(json.getString("winitProductCode"));
        productInfo.setWinitProductName(json.getString("winitProductName"));
        target.setProductInfo(productInfo);
        // 仓库信息
        WarehouseInfo warehouseInfo = new WarehouseInfo();
        warehouseInfo.setWarehouseCode(json.getString("dispatchWhCode"));
        warehouseInfo.setWarehouseName(json.getString("dispatchWhName"));
        target.setDispatchWarehouseInfo(warehouseInfo);

        BuyerInfo info = new BuyerInfo();
        BuyerInfo oraginalInfo = new BuyerInfo();
        // 收件人邮编
        info.setBuyerPostcode(json.getString("buyerPostcode"));
        oraginalInfo.setBuyerPostcode(json.getString("buyerPostcode"));
        // 收件人国家
        info.setBuyerCountry(json.getString("buyerCountry"));
        oraginalInfo.setBuyerCountry(json.getString("buyerCountry"));
        // 城市
        info.setBuyerCity(json.getString("buyerCity"));
        oraginalInfo.setBuyerCity(json.getString("buyerCity"));
        // 州、省
        info.setBuyerState(json.getString("buyerState"));
        oraginalInfo.setBuyerState(json.getString("buyerState"));
        // 收货人姓名
        info.setBuyerName(json.getString("buyerName"));
        oraginalInfo.setBuyerName(json.getString("buyerName"));
        // 收货人电话
        info.setBuyerPhone(json.getString("buyerPhone"));
        oraginalInfo.setBuyerPhone(json.getString("buyerPhone"));

        if (jsons.containsKey("buyerAddress1")) {
            String buyerAddress1 = json.getString("buyerAddress1");
            if (StringUtils.isNotBlank(buyerAddress1)) {
                buyerAddress1 = buyerAddress1.replaceAll("\n", "");
                oraginalInfo.setBuyerAddress1(buyerAddress1);
                info.setBuyerAddress1(buyerAddress1);
            }
        }
        if (jsons.containsKey("buyerAddress2")) {
            String buyerAddress2 = json.getString("buyerAddress2");
            if (StringUtils.isNotBlank(buyerAddress2)) {
                buyerAddress2 = buyerAddress2.replaceAll("\n", "");
                oraginalInfo.setBuyerAddress2(buyerAddress2);
                info.setBuyerAddress2(buyerAddress2);
            }
        }

        info.setBuyerEmail(json.getString("buyerEmail"));
        oraginalInfo.setBuyerEmail(json.getString("buyerEmail"));

        info.setBuyerHouseNo(json.getString("buyerHouseNo"));
        oraginalInfo.setBuyerHouseNo(json.getString("buyerHouseNo"));
        target.setBuyerInfoVo(info);
        target.setOriginalBuyerInfo(oraginalInfo);

        // ebay信息
        EbayOrderVo ebayOrderVo = new EbayOrderVo();
        ebayOrderVo.setIsEbay(json.getString("isEbay"));
        ebayOrderVo.setEbaySellerId(StringUtils.isNotBlank(json.getString("ebaySellerId")) ? json.getString("ebaySellerId")
            .trim() : null);
        ebayOrderVo.setEbayBuyerId(StringUtils.isNotBlank(json.getString("ebayBuyerId")) ? json.getString("ebayBuyerId")
            .trim() : null);
        target.setEbayOrderVo(ebayOrderVo);

        // 商品信息
        List<OutboundOrderMerchandiseVo> merchandiseInfoList = new ArrayList<OutboundOrderMerchandiseVo>();
        JSONArray array = json.getJSONArray("merchandiseInfoList");
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            OutboundOrderMerchandiseVo merchandiseVo = new OutboundOrderMerchandiseVo();
            merchandiseVo.setEstimateDispatchQty(obj.getInteger("qty"));
            merchandiseVo.setMerchandiseCode(obj.getString("merchandiseCode"));
            merchandiseVo.setEbayItemId(StringUtils.isNotBlank(obj.getString("ebayItemId")) ? obj.getString("ebayItemId")
                .trim() : null);
            merchandiseVo.setEbayTransactionId(StringUtils.isNotBlank(obj.getString("ebayTransactionId")) ? obj.getString("ebayTransactionId")
                .trim() : null);
            merchandiseVo.setSpecification(obj.getString("specification"));
            merchandiseInfoList.add(merchandiseVo);
        }
        target.setMerchandiseInfoList(merchandiseInfoList);

        target.setOrderNo(json.getString("orderNo"));
        target.setInvoiceUrl(json.getString("invoiceUrl"));
        target.setSellerOrderNo(json.getString("customerOrderNo"));
        // 是否重复订单
        target.setIsRepeat(StringUtils.isBlank(json.getString("isRepeat")) ? "N" : json.getString("isRepeat"));
        target.setTakeTime(json.getString("TakeTime"));
        //增值服务
        JSONArray vasarray = json.getJSONArray("VASList");
        if (null != vasarray && vasarray.size() > 0) {
            List<OutboundOrderValueAddedServiceVo> vasList = new ArrayList<OutboundOrderValueAddedServiceVo>();
            for (Object item : vasarray) {
                JSONObject obj = (JSONObject) item;
                OutboundOrderValueAddedServiceVo vasVo = new OutboundOrderValueAddedServiceVo();
                vasVo.setServiceCode(obj.getString("serviceCode"));
                vasVo.setServiceName(obj.getString("serviceName"));
                JSONArray attributeArray = obj.getJSONArray("attrList");
                if (null == attributeArray ||attributeArray.size() == 0) {
                    continue;
                }
                List<OutboundOrderVasAttributeVo> vasAttributeVoList = new ArrayList<OutboundOrderVasAttributeVo>();
                for (Object attribute : attributeArray) {
                    JSONObject attributeobj = (JSONObject) attribute;
                    OutboundOrderVasAttributeVo attributeVo = new OutboundOrderVasAttributeVo();
                    attributeVo.setServiceCode(obj.getString("serviceCode"));
                    attributeVo.setAttributeKey(attributeobj.getString("attrCode"));
                    attributeVo.setAttributeValue(attributeobj.getString("attrValue"));
                    vasAttributeVoList.add(attributeVo);
                }
                vasVo.setVasAttributeVoList(vasAttributeVoList);
                vasList.add(vasVo);
            }
            target.setVasList(vasList);
        }
        return target;
    }

    /**
     * 一步下单
     * 
     * @param jsons
     * @return
     */
    public static OutboundOrderVo buildVo(JSONObject jsons, RequestMsg requestMsg) {
        JsonValid json = new JsonValid(jsons);
        OutboundOrderVo target = new OutboundOrderVo();
        target.setOrderType(requestMsg.getPlatform());// 下单方式
        target.setStoreType(
                StringUtils.isBlank(json.getString("storeType")) ? null : json.getString("storeType").trim());// 平台名称
        if (StringUtils.isNotBlank(json.getString("distributorCode"))) {// 分销商编码
            CustomerInfo customerInfo = new CustomerInfo();
            customerInfo.setDistributorCode(json.getString("distributorCode"));
            target.setCustomerInfo(customerInfo);
            target.setOrderSource(OrderSource.DP.getSource());
        }
        // 订单来源
        if ("GFS".equals(requestMsg.getPlatform())) {
            target.setOrderSource(OrderSource.PS.getSource());
        }
        if (StringUtils.isEmpty(target.getOrderSource())) {
            target.setOrderSource(OrderSource.API.getSource());
        }
        // 产品信息
        ProductInfo productInfo = new ProductInfo();
        String orderType = OutboundOrderfieldValidator.getOrderType(json.getString("winitProductCode"));
        productInfo.setWinitProductCategory3(orderType);
        productInfo.setWinitProductCode(json.getString("winitProductCode"));
        target.setProductInfo(productInfo);
        // 仓库信息
        WarehouseInfo warehouseInfo = new WarehouseInfo();
        warehouseInfo.setWarehouseCode(json.getString("warehouseCode"));
        target.setDispatchWarehouseInfo(warehouseInfo);

        BuyerInfo info = new BuyerInfo();
        // 收件人邮编
        info.setBuyerPostcode(json.getString("receiverPostcode"));
        // 收件人国家
        info.setBuyerCountry(json.getString("receiverCountry"));
        // 城市
        info.setBuyerCity(json.getString("receiverCity"));
        // 州、省
        info.setBuyerState(json.getString("receiverState"));
        // 收货人姓名
        info.setBuyerName(json.getString("receiverName"));
        // 收货人电话
        info.setBuyerPhone(json.getString("receiverPhone"));

        if (jsons.containsKey("receiverAddressLine1")) {
            String buyerAddress1 = json.getString("receiverAddressLine1");
            if (StringUtils.isNotBlank(buyerAddress1)) {
                buyerAddress1 = buyerAddress1.replaceAll("\n", "");
                buyerAddress1 = buyerAddress1.replaceAll("\r", "");
                info.setBuyerAddress1(buyerAddress1);
            }
        }
        if (jsons.containsKey("receiverAddressLine2")) {
            String buyerAddress2 = json.getString("receiverAddressLine2");
            if (StringUtils.isNotBlank(buyerAddress2)) {
                buyerAddress2 = buyerAddress2.replaceAll("\n", "");
                buyerAddress2 = buyerAddress2.replaceAll("\r", "");
                info.setBuyerAddress2(buyerAddress2);
            }
        }
        // info.setBuyerAddress1(json.getString("receiverAddressLine1"));
        // info.setBuyerAddress2(json.getString("receiverAddressLine2"));
        info.setBuyerEmail(json.getString("receiverEmail"));
        info.setBuyerHouseNo(json.getString("receiverHouseNo"));
        target.setBuyerInfoVo(info);
        if (OrderType.SP.getType().equals(orderType)) {
            // 取件人信息 原数据
            BuyerInfo originalBuyerInfo = new BuyerInfo();
            originalBuyerInfo.setBuyerName(json.getString("receiverName"));
            originalBuyerInfo.setBuyerPhone(json.getString("receiverPhone"));
            target.setOriginalBuyerInfo(originalBuyerInfo);
        }
        // ebay信息
        EbayOrderVo ebayOrderVo = new EbayOrderVo();
        // 控制标准出库单的时候,才会保存EbaySellerId和EbayBuyerId
        if (OrderType.SD.getType().equals(orderType)) {
            ebayOrderVo.setEbaySellerId(StringUtils.isNotBlank(json.getString("eBaySellerId")) ? json.getString("eBaySellerId")
                .trim() : null);
            ebayOrderVo.setEbayBuyerId(StringUtils.isNotBlank(json.getString("eBayBuyerId")) ? json.getString("eBayBuyerId")
                .trim() : null);
        }
        String isEbay = "N";
        // 商品信息
        List<OutboundOrderMerchandiseVo> merchandiseInfoList = new ArrayList<OutboundOrderMerchandiseVo>();
        JSONArray array = json.getJSONArray("merchandiseList");
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            OutboundOrderMerchandiseVo merchandiseVo = new OutboundOrderMerchandiseVo();
            merchandiseVo.setMerchandiseCode(obj.getString("merchandiseCode"));
            merchandiseVo.setSpecification(obj.getString("specification"));
            merchandiseVo.setEstimateDispatchQty(obj.getInteger("quantity"));
            // 控制标准出库单的时候,才会保存EbayItemId和EbayTransactionId
            if (OrderType.SD.getType().equals(orderType)) {
                merchandiseVo.setEbayItemId(StringUtils.isNotBlank(obj.getString("eBayItemId")) ? obj.getString("eBayItemId")
                    .trim() : null);
                merchandiseVo.setEbayTransactionId(StringUtils.isNotBlank(obj.getString("eBayTransactionId")) ? obj.getString("eBayTransactionId")
                    .trim() : null);
            }
            // 判断如果itemID和TransactionId都不为空，就默认设置平台名称为ebay
            if (StringUtils.isNotBlank(obj.getString("eBayItemId"))
                    && StringUtils.isNotBlank(obj.getString("eBayTransactionId"))
                    && StringUtils.isBlank(target.getStoreType())) {
                target.setStoreType("ebay");// 平台名称
            }
            if (StringUtils.isNotEmpty(obj.getString("eBayTransactionId"))
                || StringUtils.isNotEmpty(obj.getString("eBayItemId"))) {
                isEbay = "Y";
            }
            merchandiseInfoList.add(merchandiseVo);
        }
        ebayOrderVo.setIsEbay(isEbay);
        target.setEbayOrderVo(ebayOrderVo);
        target.setMerchandiseInfoList(merchandiseInfoList);
        target.setOrderNo(json.getString("orderNo"));
        target.setInvoiceUrl(json.getString("invoiceUrl"));
        target.setSellerOrderNo(StringUtils.isBlank(json.getString("shipperOrderNo")) ? null : json.getString("shipperOrderNo")
            .trim());
        target.setIsRepeat(StringUtils.isBlank(json.getString("isRepeat")) ? "N" : json.getString("isRepeat"));
       //增值服务
        JSONArray vasarray = json.getJSONArray("VASList");
        if (null != vasarray && vasarray.size() > 0) {
            List<OutboundOrderValueAddedServiceVo> vasList = new ArrayList<OutboundOrderValueAddedServiceVo>();
            for (Object item : vasarray) {
                JSONObject obj = (JSONObject) item;
                OutboundOrderValueAddedServiceVo vasVo = new OutboundOrderValueAddedServiceVo();
                vasVo.setServiceCode(obj.getString("serviceCode"));
                JSONArray attributeArray = obj.getJSONArray("attrList");
                if (null == attributeArray || attributeArray.size() == 0) {
                    //throw new ApiException(ErrorCode.FIELD_NOT_NULL, "attrList");
                    vasList.add(vasVo);
                    continue;
                }
                List<OutboundOrderVasAttributeVo> vasAttributeVoList = new ArrayList<OutboundOrderVasAttributeVo>();
                for (Object attribute : attributeArray) {
                    JSONObject attributeobj = (JSONObject) attribute;
                    OutboundOrderVasAttributeVo attributeVo = new OutboundOrderVasAttributeVo();
                    attributeVo.setAttributeKey(attributeobj.getString("attrCode"));
                    attributeVo.setAttributeValue(attributeobj.getString("attrValue"));
                    vasAttributeVoList.add(attributeVo);
                }
                vasVo.setVasAttributeVoList(vasAttributeVoList);
                vasList.add(vasVo);
            }
            target.setVasList(vasList);
        }
        return target;
    }

    /**
     * 校验创建海外仓出库单参数 第一步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveFirstStep
     * 
     * @param json
     */
    public static OutboundOrderVo buildFirstStep(JSONObject jsons) {
        JsonValid json = new JsonValid(jsons);
        String orderType = json.getString("orderType");
        OutboundOrderVo vo = new OutboundOrderVo();
        vo.setOrderNo(json.getString("orderNo"));

        BuyerInfo info = new BuyerInfo();
        BuyerInfo originalBuyerInfo = new BuyerInfo();
        ProductInfo productInfo = new ProductInfo();
        productInfo.setWinitProductCategory3(orderType);
        vo.setProductInfo(productInfo);

        // 仓库信息
        WarehouseInfo warehouseInfo = new WarehouseInfo();
        warehouseInfo.setWarehouseCode(json.getString("dispatchWhCode"));
        warehouseInfo.setWarehouseName(json.getString("dispatchWhName"));
        vo.setDispatchWarehouseInfo(warehouseInfo);
        // 订单来源
        vo.setOrderSource(OrderSource.MO.getSource());
        vo.setSellerOrderNo(json.getString("customerOrderNo"));// 卖家订单
        vo.setIsRepeat("Y".equals(json.getString("isRepeat")) ? "Y" : "N");// 默认为不允许重复
        // 标准出库验证
        if (OrderType.SD.getType().equals(orderType)) {
            // 收件人邮编
            info.setBuyerPostcode(json.getString("buyerPostcode"));
            // 收件人国家
            info.setBuyerCountry(json.getString("buyerCountry"));
            // 城市
            info.setBuyerCity(json.getString("buyerCity"));
            // 州、省
            info.setBuyerState(json.getString("buyerState"));

            // 收件人邮编(原)
            originalBuyerInfo.setBuyerPostcode(json.getString("buyerPostcode"));
            // 收件人国家(原)
            originalBuyerInfo.setBuyerCountry(json.getString("buyerCountry"));
            // 城市(原)
            originalBuyerInfo.setBuyerCity(json.getString("buyerCity"));
            // 州、省(原)
            originalBuyerInfo.setBuyerState(json.getString("buyerState"));

            vo.setOriginalBuyerInfo(originalBuyerInfo);
            vo.setBuyerInfoVo(info);

            EbayOrderVo ebayOrderVo = new EbayOrderVo();
            ebayOrderVo.setIsEbay(json.getString("isEbay"));
            ebayOrderVo.setEbaySellerId(StringUtils.isNotBlank(json.getString("ebaySellerId")) ? json.getString("ebaySellerId")
                .trim() : null);
            ebayOrderVo.setEbayBuyerId(StringUtils.isNotBlank(json.getString("ebayBuyerId")) ? json.getString("ebayBuyerId")
                .trim() : null);
            vo.setEbayOrderVo(ebayOrderVo);
        } else if (OrderType.SP.getType().equals(orderType)) {
            // 收货人姓名
            info.setBuyerName(json.getString("buyerName"));
            // 收货人电话
            info.setBuyerPhone(json.getString("buyerPhone"));

            // 收货人姓名(原)
            originalBuyerInfo.setBuyerName(json.getString("buyerName"));
            // 收货人电话(原)
            originalBuyerInfo.setBuyerPhone(json.getString("buyerPhone"));

            vo.setTakeTime(json.getString("takeTime"));

            vo.setOriginalBuyerInfo(originalBuyerInfo);
            vo.setBuyerInfoVo(info);
        } else if (OrderType.DS.getType().equals(orderType)) {
            vo.setReturnOrderNo(json.getString("returnOrderNo"));
            vo.setIsReturnDestroy(json.getString("isReturnDestroy"));
        }
        return vo;
    }

    /**
     * 校验创建海外仓出库单参数 第二步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveSecondStep
     * 
     * @param json
     */
    public static OutboundOrderVo buildSecondStep(JSONObject jsons) {
        JsonValid json = new JsonValid(jsons);
        OutboundOrderVo vo = new OutboundOrderVo();
        vo.setOrderNo(json.getString("orderNo"));
        vo.setIsAcrossWarehouse(json.getString("isAcrossWarehouse"));// 是否跨仓发货
        List<OutboundOrderMerchandiseVo> merchandiseInfoList = new ArrayList<OutboundOrderMerchandiseVo>();
        JSONArray array = json.getJSONArray("merchandiseInfoList");
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            OutboundOrderMerchandiseVo merchandiseVo = new OutboundOrderMerchandiseVo();
            merchandiseVo.setOrderNo(json.getString("orderNo"));
            merchandiseVo.setEstimateDispatchQty(obj.getInteger("qty"));
            merchandiseVo.setMerchandiseCode(obj.getString("merchandiseCode"));
            merchandiseVo.setEbayItemId(StringUtils.isNotBlank(obj.getString("ebayItemId")) ? obj.getString("ebayItemId")
                .trim() : null);
            merchandiseVo.setEbayTransactionId(StringUtils.isNotBlank(obj.getString("ebayTransactionId")) ? obj.getString("ebayTransactionId")
                .trim() : null);
            if(StringUtils.isNotBlank(obj.getString("ebayItemId")) && StringUtils.isNotBlank(obj.getString("ebayTransactionId"))){
                vo.setStoreType("ebay");// 平台名称
            }
            merchandiseVo.setSpecification(obj.getString("specification"));
            merchandiseVo.setFromMerchandiseSerno(obj.getString("formerMerchandiseSerno"));
            merchandiseInfoList.add(merchandiseVo);
        }
        vo.setMerchandiseInfoList(merchandiseInfoList);
        return vo;
    }

    /**
     * 校验创建海外仓出库单参数 第三步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveThirdStep
     * 
     * @param json
     */
    public static OutboundOrderVo buildThirdStep(JSONObject jsons) {
        JsonValid json = new JsonValid(jsons);
        OutboundOrderVo vo = new OutboundOrderVo();
        vo.setOrderNo(json.getString("orderNo"));
        ProductInfo productInfo = new ProductInfo();
        productInfo.setWinitProductCode(json.getString("winitProductCode"));
        vo.setProductInfo(productInfo);
        vo.setInvoiceUrl(json.getString("invoiceUrl"));
        vo.setTakeTime(json.getString("takeTime"));
        //增值服务
        JSONArray array = json.getJSONArray("VASList");
        if (null != array && array.size() > 0) {
            List<OutboundOrderValueAddedServiceVo> vasList = new ArrayList<OutboundOrderValueAddedServiceVo>();
            for (Object item : array) {
                JSONObject obj = (JSONObject) item;
                OutboundOrderValueAddedServiceVo vasVo = new OutboundOrderValueAddedServiceVo();
                vasVo.setServiceCode(obj.getString("serviceCode"));
                JSONArray attributeArray = obj.getJSONArray("attrList");
                if (null == attributeArray ||attributeArray.size() == 0) {
                    //throw new ApiException(ErrorCode.FIELD_NOT_NULL, "attrList");
                    vasList.add(vasVo);
                    continue;
                }
                List<OutboundOrderVasAttributeVo> vasAttributeVoList = new ArrayList<OutboundOrderVasAttributeVo>();
                for (Object attribute : attributeArray) {
                    JSONObject attributeobj = (JSONObject) attribute;
                    OutboundOrderVasAttributeVo attributeVo = new OutboundOrderVasAttributeVo();
                    attributeVo.setAttributeKey(attributeobj.getString("attrCode"));
                    attributeVo.setAttributeValue(attributeobj.getString("attrValue"));
                    vasAttributeVoList.add(attributeVo);
                }
                vasVo.setVasAttributeVoList(vasAttributeVoList);
                vasList.add(vasVo);
            }
            vo.setVasList(vasList);
        }
        return vo;
    }

    /**
     * 校验创建海外仓出库单参数 第四步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveFouthStep
     * 
     * @param json
     */
    public static OutboundOrderVo buildFouthStep(JSONObject jsons) {
        JsonValid json = new JsonValid(jsons);
        OutboundOrderVo vo = new OutboundOrderVo();
        vo.setOrderNo(json.getString("orderNo"));
        // 原买家信息
        BuyerInfo originalBuyerInfo = new BuyerInfo();
        BuyerInfo info = new BuyerInfo();

        if (jsons.containsKey("buyerAddress1")) {
            String buyerAddress1 = json.getString("buyerAddress1");
            if (StringUtils.isNotBlank(buyerAddress1)) {
                buyerAddress1 = buyerAddress1.replaceAll("\n", "");
                originalBuyerInfo.setBuyerAddress1(buyerAddress1);
                info.setBuyerAddress1(buyerAddress1);
            }
        }
        if (jsons.containsKey("buyerAddress2")) {
            String buyerAddress2 = json.getString("buyerAddress2");
            if (StringUtils.isNotBlank(buyerAddress2)) {
                buyerAddress2 = buyerAddress2.replaceAll("\n", "");
                originalBuyerInfo.setBuyerAddress2(buyerAddress2);
                info.setBuyerAddress2(buyerAddress2);
            }
        }
        originalBuyerInfo.setBuyerEmail(json.getString("buyerEmail"));
        originalBuyerInfo.setBuyerPhone(json.getString("buyerPhone"));
        originalBuyerInfo.setBuyerHouseNo(json.getString("buyerHouseNo"));
        originalBuyerInfo.setBuyerName(json.getString("buyerName"));
        vo.setOriginalBuyerInfo(originalBuyerInfo);

        info.setBuyerEmail(json.getString("buyerEmail"));
        info.setBuyerPhone(json.getString("buyerPhone"));
        info.setBuyerHouseNo(json.getString("buyerHouseNo"));
        info.setBuyerName(json.getString("buyerName"));
        vo.setBuyerInfoVo(info);
        return vo;
    }

    /**
     * 构建单步提交入参
     * 
     * @param json
     * @return
     */
    public static OutboundOrderVo buildSubmit(JSONObject json) {
        OutboundOrderVo vo = new OutboundOrderVo();
        vo.setOrderNo(json.getString("orderNo"));
        if (json.containsKey("orderType")) {
            String orderType = json.getString("orderType");
            boolean type = OutboundOrderEnumContants.OrderType.DS.getType().equals(orderType)
                           || OutboundOrderEnumContants.OrderType.SP.getType().equals(orderType);
            if (type) {
                if (json.containsKey("winitProductCode")) {
                    ProductInfo productInfo = new ProductInfo();
                    productInfo.setWinitProductCode(json.getString("winitProductCode"));
                    vo.setProductInfo(productInfo);
                }
            } else if (OutboundOrderEnumContants.OrderType.SD.getType().equals(orderType)) {
                BuyerInfo info = new BuyerInfo();
                BuyerInfo originalBuyerInfo = new BuyerInfo();
                if (json.containsKey("buyerAddress1")) {
                    String buyerAddress1 = json.getString("buyerAddress1");
                    if (StringUtils.isNotBlank(buyerAddress1)) {
                        buyerAddress1 = buyerAddress1.replaceAll("\n", "");
                        originalBuyerInfo.setBuyerAddress1(buyerAddress1);
                        info.setBuyerAddress1(buyerAddress1);
                    }
                }
                if (json.containsKey("buyerAddress2")) {
                    String buyerAddress2 = json.getString("buyerAddress2");
                    if (StringUtils.isNotBlank(buyerAddress2)) {
                        buyerAddress2 = buyerAddress2.replaceAll("\n", "");
                        originalBuyerInfo.setBuyerAddress2(buyerAddress2);
                        info.setBuyerAddress2(buyerAddress2);
                    }
                }
                if (json.containsKey("buyerEmail")) {
                    info.setBuyerEmail(json.getString("buyerEmail"));
                    originalBuyerInfo.setBuyerEmail(json.getString("buyerEmail"));
                }
                if (json.containsKey("buyerPhone")) {
                    info.setBuyerPhone(json.getString("buyerPhone"));
                    originalBuyerInfo.setBuyerPhone(json.getString("buyerPhone"));
                }
                if (json.containsKey("buyerHouseNo")) {
                    info.setBuyerHouseNo(json.getString("buyerHouseNo"));
                    originalBuyerInfo.setBuyerHouseNo(json.getString("buyerHouseNo"));
                }
                if (json.containsKey("buyerName")) {
                    info.setBuyerName(json.getString("buyerName"));
                    originalBuyerInfo.setBuyerName(json.getString("buyerName"));
                }
                vo.setOriginalBuyerInfo(originalBuyerInfo);
                vo.setBuyerInfoVo(info);
            } else {
                throw new ApiException("没有匹配的订单类型");
            }
        }
        return vo;
    }

    /**
     * 构建订单列表
     * 
     * @param vo
     * @return
     */
    public static Map<String, Object> buildOutboundOrderListPageVo(OutboundOrderVo vo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderNo", vo.getOrderNo());// 订单编号.
        map.put("sellerOrderNo", vo.getSellerOrderNo());// 卖家订单号
        map.put("status", vo.getStatus());// 订单状态
        map.put("orderTime", vo.getOrderTime());// 下单时间
        map.put("createTime", vo.getCreateDate());// 订单创建时间
        map.put("finishedTime", vo.getFinishedTime());// 完成时间
        map.put("serviceNormDay", vo.getServiceNormDay());// 服务标准天数
        map.put("serviceCompletionDay", vo.getServiceCompletionDay());// 服务完成天数
        map.put("orderSource", vo.getOrderSource());// 订单来源
        map.put("weight", vo.getWeight());// 重量
        map.put("volume", vo.getVolume());// 体积
        map.put("winitTrackingNo", vo.getWinitTrackingNo());// Winit 跟踪号
        map.put("estimateAmount", vo.getEstimateAmount());
        map.put("currencyCode", vo.getCurrencyCode());
        map.put("isStandardAddress", vo.getAddressCheckStatus());
        map.put("isStartReturn", vo.getIsStartReturn());
        map.put("causeOfFailure", vo.getCauseOfFailure());// 失败原因
        map.put("validateResult", vo.getValidateResult());// 字段校验结果
        map.put("isRepeat", vo.getIsRepeat());// 是否重复
        map.put("isAcrossWarehouse", vo.getIsAcrossWarehouse());// 是否跨仓发货
        map.put("estimateCompleteDate", vo.getEstimateCompleteDate());// 预计送达时间
        if (vo.getEbayOrderVo() != null) {
            map.put("isEbay", vo.getEbayOrderVo().getIsEbay());// 是否是ebay订单
        }
        map.put("cancelResultDesc", vo.getCancelResultDesc());// 订单取消描述
        map.put("isReturnDestroy", "Y".equals(vo.getIsReturnDestroy()) ? "Y" : "N");
        List<Map<String, Object>> merList = new ArrayList<Map<String, Object>>();
        List<OutboundOrderMerchandiseVo> merchandiseInfoList = vo.getMerchandiseInfoList();
        if (null != merchandiseInfoList && merchandiseInfoList.size() > 0) {
            for (OutboundOrderMerchandiseVo tempVo : merchandiseInfoList) {
                Map<String, Object> merMap = new HashMap<String, Object>();
                merMap.put("merchandiseCode", tempVo.getMerchandiseCode());
                merMap.put("ebayItemId", tempVo.getEbayItemId());
                merMap.put("ebayTransactionId", tempVo.getEbayTransactionId());
                merMap.put("specification", tempVo.getSpecification());
                merMap.put("qty", tempVo.getEstimateDispatchQty());
                merList.add(merMap);
            }
        }
        map.put("merList", merList);
        // 客户信息
        CustomerInfo customerInfo = vo.getCustomerInfo();
        if (customerInfo != null) {
            map.put("distributorName", customerInfo.getDistributorName());// 分销商名称
        }
        // 产品信息
        ProductInfo productInfo = vo.getProductInfo();
        if (productInfo != null) {
            map.put("orderType", productInfo.getWinitProductCategory3());
            map.put("winitProductName", productInfo.getWinitProductName());// Winit产品名称
            map.put("winitProductCode", productInfo.getWinitProductCode());// Winit产品编码
        }
        // 收件人信息(原始信息)
        BuyerInfo originalBuyerInfo = vo.getOriginalBuyerInfo();
        if (originalBuyerInfo != null) {
            map.put("buyerName", originalBuyerInfo.getBuyerName());// 收件人姓名
            map.put("buyerCountry", originalBuyerInfo.getBuyerCountry());// 收件人国家
        }
        // 收件人信息(修正后信息)
        BuyerInfo buyerInfo = vo.getBuyerInfoVo();
        if (buyerInfo != null) {
            map.put("buyerCountryCode", buyerInfo.getBuyerCountryCode());// 收件人国家编码
        }
        // ebay信息
        EbayOrderVo ebayOrderVo = vo.getEbayOrderVo();
        if (ebayOrderVo != null) {
            map.put("isEbayPlus", ebayOrderVo.getIsEbayPlus());// eBay+
            map.put("ebaySellerId", ebayOrderVo.getEbaySellerId());// eBaySellerID
            map.put("ebayBuyerId", ebayOrderVo.getEbayBuyerId());// ebayBuyerId
            map.put("EGDValidateCode", ebayOrderVo.getEGDValidateCode());// EGD校验结果编码
            map.put("EGDVldDescCode", ebayOrderVo.getEGDVldDescCode());// EGD校验结果描述编码
            map.put("EGDVldDesc", ebayOrderVo.getEGDVldDesc());// EGD校验结果编码
            map.put("GDDTime", ebayOrderVo.getGDDTime());// EGD校验结果编码
        }
        // 仓库信息
        WarehouseInfo warehouseInfo = vo.getDispatchWarehouseInfo();
        if (warehouseInfo != null) {
            map.put("warehouseCode", warehouseInfo.getWarehouseCode());// 仓库编码.
            map.put("warehouseName", warehouseInfo.getWarehouseName());// 仓库名称
            map.put("countryName", warehouseInfo.getCountryName());// 仓库所在国家名称
            map.put("countryCode", warehouseInfo.getCountryCode());// 仓库所在国家编码
        }
        List<Map<String, String>> trackingNoList = new ArrayList<Map<String, String>>();
        List<String> list = vo.getTrackingNo();
        if (null != list) {
            for (String trackingNo : list) {
                Map<String, String> traMap = new HashMap<String, String>();
                traMap.put("trackingNo", trackingNo);
                trackingNoList.add(traMap);
            }
        }
        map.put("trackingNoList", trackingNoList);
        //sla
        OutboundOrderSlaDetailVo slaVo=vo.getSlaDetailVo();
        if(null!=slaVo){
            map.put("targetDeliveredTime", slaVo.getTargetDeliveredTime());// 目标妥投时间
            map.put("actualDeliveredTime", slaVo.getActualDeliveredTime());// 实际妥投时间
            map.put("targetOutTime", slaVo.getTargetOutTime());// 应出库时间
            map.put("actualOutTime", slaVo.getActualOutTime());// 实际出库时间
            map.put("ontimeTrack", slaVo.getOntimeTrack());// 准时派送.
        }
        return map;
    }

    /**
     * 构建导出订单列表
     * 
     * @param vo
     * @return
     */
    public static Map<String, Object> buildExportOrderList(OutboundOrderVo vo) {
        Map<String, Object> map = buildOutboundOrderListPageVo(vo);
        CustomerInfo customerInfo = vo.getCustomerInfo();
        if (customerInfo != null) {
            map.put("customerName", customerInfo.getCustomerName());
        }
        map.put("deliveryName", vo.getVendorName());// 派送商名称
        BuyerInfo buyerInfo = vo.getOriginalBuyerInfo();
        if (buyerInfo != null) {
            map.put("buyerCity", buyerInfo.getBuyerCity());
            map.put("buyerHouseNo", buyerInfo.getBuyerHouseNo());
            map.put("buyerName", buyerInfo.getBuyerName());
            map.put("buyerAddress1", buyerInfo.getBuyerAddress1());
            map.put("buyerAddress2", buyerInfo.getBuyerAddress2());
            map.put("buyerState", buyerInfo.getBuyerState());
            map.put("postCode", buyerInfo.getBuyerPostcode());
            map.put("buyerMail", buyerInfo.getBuyerEmail());
            map.put("buyerPhone", buyerInfo.getBuyerPhone());
        }
        List<OutboundOrderRevenueVo> revenueVoList = vo.getRevenueVoList();
        if (null == revenueVoList || revenueVoList.size() == 0) {
            return map;
        }
        /*
         * List<Map<String, Object>> revenueList = new ArrayList<Map<String,
         * Object>>(); for (OutboundOrderRevenueVo revenueVo : revenueVoList) {
         * Map<String, Object> revenueMap = new HashMap<String, Object>();
         * revenueMap.put("chargeWeight", revenueVo.getChargeWeight());
         * revenueMap.put("chargeType", revenueVo.getUnit());
         * revenueMap.put("amount", revenueVo.getAmount());
         * revenueMap.put("standardCurrency",
         * revenueVo.getStandardCurrencyCode());
         * revenueMap.put("balanceExchangeRate", revenueVo.getExRate());
         * revenueMap.put("balanceAmmount", revenueVo.getCurrencyAmount());
         * revenueList.add(revenueMap); } map.put("revenueList", revenueList);
         */
        return map;
    }

    /**
     * 构建出库单修改信息(只提供修改收件人信息).
     * 
     * @param json
     * @return
     */
    public static OutboundOrderVo buildModifyVo(JSONObject json) {
        OutboundOrderVo vo = new OutboundOrderVo();
        JSONObject buyerInfoJson = json.getJSONObject("buyerInfoVo");
        BuyerInfo buyerInfoVo = new BuyerInfo();
        vo.setOrderNo(json.getString("orderNo"));
        buyerInfoVo.setBuyerName(buyerInfoJson.getString("buyerName"));
        buyerInfoVo.setBuyerEmail(buyerInfoJson.getString("buyerEmail"));
        buyerInfoVo.setBuyerPhone(buyerInfoJson.getString("buyerPhone"));
        buyerInfoVo.setBuyerHouseNo(buyerInfoJson.getString("buyerHouseNo"));
        buyerInfoVo.setBuyerCountry(buyerInfoJson.getString("buyerCountry"));
        buyerInfoVo.setBuyerState(buyerInfoJson.getString("buyerState"));
        buyerInfoVo.setBuyerCity(buyerInfoJson.getString("buyerCity"));
        buyerInfoVo.setBuyerPostcode(buyerInfoJson.getString("buyerPostcode"));
        buyerInfoVo.setBuyerAddress1(buyerInfoJson.getString("buyerAddress1"));
        buyerInfoVo.setBuyerAddress2(buyerInfoJson.getString("buyerAddress2"));
        vo.setBuyerInfoVo(buyerInfoVo);
        return vo;
    }

    public static QueryOrderBySellerCommand buildOutboundCommand(JSONObject json) {
        QueryOrderBySellerCommand command = new QueryOrderBySellerCommand();
        OutboundOrderVo vo = new OutboundOrderVo();
        if (json.containsKey("orderNo")) {// 订单号
            vo.setOrderNo(json.getString("orderNo"));
        }
        if (json.containsKey("sellerOrderNo")) {// 卖家订单号
            vo.setSellerOrderNo(json.getString("sellerOrderNo"));
        }
        if (json.containsKey("winitTrackingNo")) {// winit跟踪号
            vo.setWinitTrackingNo(json.getString("winitTrackingNo"));
        }
        if (json.containsKey("orderStatus")) {// 订单状态
            vo.setStatus(json.getString("orderStatus"));
        }
        // 收件人信息
        if (json.containsKey("buyerName")) {// 收件人姓名
            BuyerInfo buyerInfo = new BuyerInfo();
            buyerInfo.setBuyerName(json.getString("buyerName"));
            vo.setBuyerInfoVo(buyerInfo);
        }
        if (json.containsKey("dispatchWhCode")) {// 发货仓编号
            WarehouseInfo warehouseInfo = new WarehouseInfo();
            warehouseInfo.setWarehouseCode(json.getString("dispatchWhCode"));
            vo.setDispatchWarehouseInfo(warehouseInfo);
        }
        if (json.containsKey("trackingNo")) {// 快递单号
            List<String> tList = new ArrayList<String>();
            tList.add(json.getString("trackingNo"));
            vo.setTrackingNo(tList);
        }
        List<OutboundOrderMerchandiseVo> mList = new ArrayList<OutboundOrderMerchandiseVo>();
        OutboundOrderMerchandiseVo mvo = new OutboundOrderMerchandiseVo();
        if (json.containsKey("merchandiseCode")) {// 商品编码
            mvo.setMerchandiseCode(json.getString("merchandiseCode"));
        }
        if (json.containsKey("merchandiseName")) {// 商品名称
            mvo.setNameCn(json.getString("merchandiseName"));
        }
        mList.add(mvo);
        vo.setMerchandiseInfoList(mList);
        if (json.containsKey("orderSource")) {// 订单来源
            vo.setOrderSource(json.getString("orderSource"));
        }
        if (json.containsKey("orderType")) {// 订单类型
            ProductInfo productInfo = new ProductInfo();
            productInfo.setWinitProductCategory3(json.getString("orderType"));
            vo.setProductInfo(productInfo);
        }
        CustomerInfo customerInfo = new CustomerInfo();
        if (json.containsKey("customerName")) {// 客户名称（下单人）
            customerInfo.setCustomerName(json.getString("customerName"));
        }
        if (json.containsKey("distributorName")) {// 分销商名称
            customerInfo.setDistributorName(json.getString("distributorName"));
        }
        if (StringUtils.isNoneBlank(customerInfo.getCustomerName())
            || StringUtils.isNoneBlank(customerInfo.getDistributorName())) {
            vo.setCustomerInfo(customerInfo);
        }
        if (json.containsKey("validateResult")) {// 上传验证结果
            vo.setValidateResult(json.getString("validateResult"));
        }
        if (json.containsKey("isAcrossWarehouse")) {
            vo.setIsAcrossWarehouse(json.getString("isAcrossWarehouse"));
        }
        EbayOrderVo ebayOrderVo = new EbayOrderVo();
        if (json.containsKey("isEbay")) {// 是否是ebay订单
            ebayOrderVo.setIsEbay(json.getString("isEbay"));
            vo.setEbayOrderVo(ebayOrderVo);
        }
        if (json.containsKey("ebaySellerId")) { // 卖家ebayId
            ebayOrderVo.setEbaySellerId(json.getString("ebaySellerId"));
            vo.setEbayOrderVo(ebayOrderVo);
        }
        if (json.containsKey("eGDValidateCode")) {// egd校验状态
            ebayOrderVo.setEGDValidateCode(json.getString("eGDValidateCode"));
            vo.setEbayOrderVo(ebayOrderVo);
        }
        // 转为时间类型.
        if (json.containsKey("orderDateFrom")) {// 开始时间
            String orderDateStart = json.getString("orderDateFrom");
            orderDateStart += " 00:00:00";
            Date from = DateUtil.parse2Date(orderDateStart, DateUtil.FULL_DATE_STRING);
            vo.setOrderStartDate(from);
        }
        if (json.containsKey("orderDateFromTo")) {// 结束时间
            String orderDateEnd = json.getString("orderDateFromTo");
            orderDateEnd += " 23:59:59";
            Date end = DateUtil.parse2Date(orderDateEnd, DateUtil.FULL_DATE_STRING);
            vo.setOrderEndDate(end);
        }
        command.setOutboundOrderVo(vo);
        return command;
    }

    public static Map<String, Object> buildReturnOutboundOrderInfo(OutboundOrderVo vo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderNo", vo.getOrderNo() == null ? "" : vo.getOrderNo());// 订单编号
        String status = vo.getStatus();
        if (StringUtils.isNotBlank(status)) {
            map.put("status", vo.getStatus());// 订单状态
        }
        map.put("orderTime", vo.getOrderTime() == null ? "" : vo.getOrderTime());// 下单时间
        map.put("outWhTime", vo.getOutWhTime() == null ? "" : vo.getOutWhTime());// 出库时间
        map.put("finishedTime", vo.getFinishedTime() == null ? "" : vo.getFinishedTime());// 完成时间
        map.put("ontimeTrack", vo.getOntimeTrack() == null ? "" : vo.getOntimeTrack());// 准时派送
        map.put("serviceNormDay", vo.getServiceNormDay() == null ? "" : vo.getServiceNormDay());// 服务标准天数
        map.put("serviceCompletionDay", vo.getServiceCompletionDay() == null ? "" : vo.getServiceCompletionDay());// 服务完成天数
        map.put("orderSource", vo.getOrderSource() == null ? "" : vo.getOrderSource());// 订单来源.
        map.put("packageTypeNo", vo.getPackageTypeNo() == null ? "" : vo.getPackageTypeNo());//
        map.put("invoiceUrl", vo.getInvoiceUrl() == null ? "" : vo.getInvoiceUrl());// 客户发票.
        map.put("cancelContent", vo.getCancelContent() == null ? "" : vo.getCancelContent());// 取消原因
        map.put("cancelResultDesc", vo.getCancelResultDesc());// 订单取消描述
        map.put("volume", vo.getVolume() == null ? "" : vo.getVolume());// 体积
        map.put("weight", vo.getWeight() == null ? "" : vo.getWeight());// 重量
        map.put("takeTime", vo.getTakeTime() == null ? "" : vo.getTakeTime());
        map.put("winitTrackingNo", vo.getWinitTrackingNo() == null ? "" : vo.getWinitTrackingNo());// winit跟踪号
        map.put("sellerOrderNo", vo.getSellerOrderNo() == null ? "" : vo.getSellerOrderNo());// 卖家订单号
        map.put("carrier", vo.getCarrierCode() == null ? "" : vo.getCarrierCode());// 承运人编码(ebay显示名称)
        map.put("isRepeat", vo.getIsRepeat() == null ? "" : vo.getIsRepeat());// 是否允许重复
        map.put("deliveryCompletionStatus", vo.getOntimeTrack() == null ? "" : vo.getOntimeTrack());// 派送状态(准时：PT
                                                                                                    // 迟到：BL
                                                                                                    // 进行中：IP)
        map.put("isStandardAddress", vo.getAddressCheckStatus());
        map.put("trayQty", vo.getTrayQtyQ());// 托盘数
        map.put("isAcrossWarehouse", vo.getIsAcrossWarehouse());// 订单取消描述
        map.put("estimateCompleteDate", vo.getEstimateCompleteDate());// 预计送达时间
        map.put("storeType",StringUtils.isBlank(vo.getStoreType()) ? "" : vo.getStoreType());// 平台名称
        if (vo.getCommonPictureList() != null && vo.getCommonPictureList().size() > 0) {
            StringBuilder signPicture = new StringBuilder();
            for (String picUrl : vo.getCommonPictureList()) {
                signPicture.append(picUrl).append(";");
            }
            map.put("signPicture", signPicture.toString());// 签收图片
        }
        // 客户信息
        CustomerInfo customerInfo = vo.getCustomerInfo();
        if (customerInfo != null) {
            map.put("distributorName", customerInfo.getDistributorName());
        }
        // 仓库信息
        WarehouseInfo warehouseInfo = vo.getDispatchWarehouseInfo();
        if (warehouseInfo != null) {
            map.put("warehouseCode", warehouseInfo.getWarehouseCode() == null ? "" : warehouseInfo.getWarehouseCode());// 仓库编码
            map.put("warehouseName", warehouseInfo.getWarehouseName() == null ? "" : warehouseInfo.getWarehouseName());// 仓库名称
            map.put("countryCode", warehouseInfo.getCountryCode() == null ? "" : warehouseInfo.getCountryCode());// 国家编码
            map.put("countryName", warehouseInfo.getCountryName() == null ? "" : warehouseInfo.getCountryName());// 国家名称
        }
        // 产品信息
        ProductInfo productInfo = vo.getProductInfo();
        if (vo.getProductInfo() != null) {
            map.put("winitProductCode",
                productInfo.getWinitProductCode() == null ? "" : productInfo.getWinitProductCode());
            map.put("winitProductName",
                productInfo.getWinitProductName() == null ? "" : productInfo.getWinitProductName());
            map.put("exportType", productInfo.getExportType() == null ? "" : productInfo.getExportType());// 出库方式
            map.put("orderType",
                productInfo.getWinitProductCategory3() == null ? "" : productInfo.getWinitProductCategory3());
        }
        // ebay信息
        EbayOrderVo ebayOrderVo = vo.getEbayOrderVo();
        if (ebayOrderVo != null) {
            map.put("isEbay", ebayOrderVo.getIsEbay() == null ? "" : ebayOrderVo.getIsEbay());
            map.put("isEbayPlus", ebayOrderVo.getIsEbayPlus() == null ? "" : ebayOrderVo.getIsEbayPlus());
            map.put("ebayPayTime", ebayOrderVo.getEbayPayTimeStr() == null ? "" : ebayOrderVo.getEbayPayTimeStr());
            map.put("ebaySellerId", ebayOrderVo.getEbaySellerId() == null ? "" : ebayOrderVo.getEbaySellerId());
            map.put("ebayBuyerId", ebayOrderVo.getEbayBuyerId() == null ? "" : ebayOrderVo.getEbayBuyerId());
            map.put("ebayValidateResult",
                ebayOrderVo.getEbayValidateResult() == null ? "" : ebayOrderVo.getEbayValidateResult());
            map.put("EGDValidateCode", ebayOrderVo.getEGDValidateCode());// EGD校验结果编码
            map.put("EGDVldDescCode", ebayOrderVo.getEGDVldDescCode());// EGD校验结果描述编码
            map.put("EGDVldDesc", ebayOrderVo.getEGDVldDesc());// EGD校验结果编码
            map.put("GDDTime", ebayOrderVo.getGDDTime());// GDD时间
        }
        // 收货人信息.
        BuyerInfo buyerInfo = vo.getOriginalBuyerInfo();
        if (null != buyerInfo) {
            map.put("buyerName", buyerInfo.getBuyerName() == null ? "" : buyerInfo.getBuyerName());
            map.put("buyerEmail", buyerInfo.getBuyerEmail() == null ? "" : buyerInfo.getBuyerEmail());
            map.put("buyerPhone", buyerInfo.getBuyerPhone() == null ? "" : buyerInfo.getBuyerPhone());
            map.put("buyerHouseNo", buyerInfo.getBuyerHouseNo() == null ? "" : buyerInfo.getBuyerHouseNo());
            map.put("buyerCountry", buyerInfo.getBuyerCountry() == null ? "" : buyerInfo.getBuyerCountry());
            map.put("buyerState", buyerInfo.getBuyerState() == null ? "" : buyerInfo.getBuyerState());
            map.put("buyerCity", buyerInfo.getBuyerCity() == null ? "" : buyerInfo.getBuyerCity());
            map.put("buyerPostcode", buyerInfo.getBuyerPostcode() == null ? "" : buyerInfo.getBuyerPostcode());
            map.put("buyerAddress1", buyerInfo.getBuyerAddress1() == null ? "" : buyerInfo.getBuyerAddress1());
            map.put("buyerAddress2", buyerInfo.getBuyerAddress2() == null ? "" : buyerInfo.getBuyerAddress2());
        }
        // 修正后收货人信息
        BuyerInfo correctionBuyerInfo = vo.getBuyerInfoVo();
        if (null != correctionBuyerInfo) {
            map.put("correctionBuyerName", buyerInfo.getBuyerName() == null ? "" : correctionBuyerInfo.getBuyerName());
            map.put("correctionBuyerEmail", buyerInfo.getBuyerEmail() == null ? "" : correctionBuyerInfo.getBuyerEmail());
            map.put("correctionBuyerPhone", buyerInfo.getBuyerPhone() == null ? "" : correctionBuyerInfo.getBuyerPhone());
            map.put("correctionBuyerHouseNo", buyerInfo.getBuyerHouseNo() == null ? "" : correctionBuyerInfo.getBuyerHouseNo());
            map.put("correctionBuyerCountry", buyerInfo.getBuyerCountry() == null ? "" : correctionBuyerInfo.getBuyerCountry());
            map.put("correctionBuyerState", buyerInfo.getBuyerState() == null ? "" : correctionBuyerInfo.getBuyerState());
            map.put("correctionBuyerCity", buyerInfo.getBuyerCity() == null ? "" : correctionBuyerInfo.getBuyerCity());
            map.put("correctionBuyerPostcode", buyerInfo.getBuyerPostcode() == null ? "" : correctionBuyerInfo.getBuyerPostcode());
            map.put("correctionBuyerAddress1", buyerInfo.getBuyerAddress1() == null ? "" : correctionBuyerInfo.getBuyerAddress1());
            map.put("correctionBuyerAddress2", buyerInfo.getBuyerAddress2() == null ? "" : correctionBuyerInfo.getBuyerAddress2());
        }
        // 供应商跟踪号
        List<String> trackings = vo.getTrackingNo();
        List<Map<String, String>> vendorTrackingNos = new ArrayList<Map<String, String>>();
        if (null != trackings && trackings.size() > 0) {
            for (String trackingNo : trackings) {
                Map<String, String> trackingMap = new HashMap<String, String>();
                trackingMap.put("vendorTrackingNo", trackingNo);
                vendorTrackingNos.add(trackingMap);
            }
        }
        map.put("vendorTrackingNos", vendorTrackingNos);
        // 服务信息
        List<OutboundOrderSlaVo> slaList = vo.getSlaList();
        List<Map<String, Object>> serviceMapList = new ArrayList<Map<String, Object>>();
        if (slaList != null && slaList.size() > 0) {
            for (OutboundOrderSlaVo slaVo : slaList) {
                Map<String, Object> serviceMap = new HashMap<String, Object>();
                serviceMap.put("serviceStatus", slaVo.getStatus() == null ? "" : slaVo.getStatus());
                serviceMap.put("serviceName", slaVo.getSlaName() == null ? "" : slaVo.getSlaName());
                serviceMap.put("standardTime",
                    slaVo.getServiceStandardTime() == null ? "" : slaVo.getServiceStandardTime());
                serviceMap.put("completeTime",
                    slaVo.getServiceCompletionTime() == null ? "" : slaVo.getServiceCompletionTime());
                serviceMapList.add(serviceMap);
            }
        }
        map.put("serviceList", serviceMapList);
        // 地址校验结果
        OutboundOrderAddressCheckTheResultVo addresReslut = vo.getAddressCheckTheResultVo();
        if (null != addresReslut) {
            map.put("result", addresReslut.getResult() == null ? "" : addresReslut.getResult());
            map.put("resultMessage", addresReslut.getResultMessage() == null ? "" : addresReslut.getResultMessage());
            map.put("resultAddressCity", addresReslut.getCity() == null ? "" : addresReslut.getCity());
            map.put("resultAddressStreets", addresReslut.getStreets() == null ? "" : addresReslut.getStreets());
            map.put("resultAddressPostcode", addresReslut.getPostcode() == null ? "" : addresReslut.getPostcode());
        }
        return map;
    }

    /**
     * 构建单步提交入参
     * 
     * @param json
     * @return
     */
    public static OutboundOrderVo buildreOrder(JSONObject json) {
        OutboundOrderVo vo = new OutboundOrderVo();
        vo.setOrderNo(json.getString("orderNo"));
        if (json.containsKey("orderType")) {
            String orderType = json.getString("orderType");
            boolean type = OutboundOrderEnumContants.OrderType.DS.getType().equals(orderType)
                           || OutboundOrderEnumContants.OrderType.SP.getType().equals(orderType);
            if (type) {
                if (json.containsKey("winitProductCode")) {
                    ProductInfo productInfo = new ProductInfo();
                    productInfo.setWinitProductCode(json.getString("winitProductCode"));
                    vo.setProductInfo(productInfo);
                }
            } else if (OutboundOrderEnumContants.OrderType.SD.getType().equals(orderType)) {
                BuyerInfo info = new BuyerInfo();
                if (json.containsKey("buyerAddress1")) {
                    info.setBuyerAddress1(json.getString("buyerAddress1"));
                }
                if (json.containsKey("buyerAddress2")) {
                    info.setBuyerAddress2(json.getString("buyerAddress2"));
                }
                if (json.containsKey("buyerEmail")) {
                    info.setBuyerEmail(json.getString("buyerEmail"));
                }
                if (json.containsKey("buyerPhone")) {
                    info.setBuyerPhone(json.getString("buyerPhone"));
                }
                if (json.containsKey("buyerHouseNo")) {
                    info.setBuyerHouseNo(json.getString("buyerHouseNo"));
                }
                if (json.containsKey("buyerName")) {
                    info.setBuyerName(json.getString("buyerName"));
                }
                vo.setBuyerInfoVo(info);
            } else {
                throw new ApiException("没有匹配的订单类型");
            }
        }
        return vo;
    }

    /**
     * 计价工具入参
     * 
     * @param json
     * @return OutboundOrderBillingVo
     */
    public static OutboundOrderBillingVo buildValuationTool(JSONObject json) {
        OutboundOrderBillingVo vo = new OutboundOrderBillingVo();
        if (json.containsKey("warehouseCode")) {
            vo.setDeliveryWHCode(json.getString("warehouseCode"));
        }
        if (json.containsKey("countryCode")) {
            vo.setBuyerCountryCode(json.getString("countryCode"));
        }
        if (json.containsKey("weight")) {
            vo.setWeight(json.getBigDecimal("weight"));
        }
        if (json.containsKey("width")) {
            vo.setWidth(json.getBigDecimal("width"));
        }
        if (json.containsKey("destinationPostcode")) {
            vo.setDestinationPostcode(json.getString("destinationPostcode"));
        }
        if (json.containsKey("length")) {
            vo.setLength(json.getBigDecimal("length"));
        }
        if (json.containsKey("height")) {
            vo.setHeight(json.getBigDecimal("height"));
        }
        return vo;
    }
    
    /**
     * new计价工具入参
     * 
     * @param json
     * @return OutboundOrderBillingVo
     */
    public static ProductRecommendRequestParams buildNewValuationTool(JSONObject json) {
        ProductRecommendRequestParams target = new ProductRecommendRequestParams();
        RequestParamsReceiveAddress address = new RequestParamsReceiveAddress();
        if (json.containsKey("country")) {
            address.setCountry(json.getString("country").trim());
        }
        if (json.containsKey("region")) {
            address.setState(json.getString("region").trim());
        }
        if (json.containsKey("zipCode")) {
            address.setPostCode(json.getString("zipCode").trim());
        }
       
        target.setAddress(address);
        
        Bpartner partner = CommandContext.getContext().get("apiBpartner");
        target.setCustomerCode(partner.getValue());
        
        JSONArray array = json.getJSONArray("productList");
        List<RequestParamsParcels>  parcels = new ArrayList<RequestParamsParcels>();
        RequestParamsParcels parcel = new RequestParamsParcels();
        List<RequestParamsMerchandises> paramsMerchandises = new ArrayList<RequestParamsMerchandises>();
        
        for (Object item : array) {
            RequestParamsMerchandises merchandises = new RequestParamsMerchandises(); 
            JSONObject obj = (JSONObject) item;
            if (obj.containsKey("productCode")) {
                merchandises.setSKU(obj.getString("productCode").trim());
            }
            if (null != obj.get("productNum") && StringUtils.isNotBlank(obj.get("productNum").toString())) {
                merchandises.setQty(obj.getInteger("productNum"));
            } else {
                merchandises.setQty(1);
            }
            if (obj.containsKey("specification")) {
                merchandises.setSpecification(obj.getString("specification").trim());
            }
            if (obj.containsKey("length")) {
                merchandises.setLength(obj.getBigDecimal("length"));
            }
            if (obj.containsKey("width")) {
                merchandises.setWidth(obj.getBigDecimal("width"));
            }
            if (obj.containsKey("height")) { 
                merchandises.setHeight(obj.getBigDecimal("height"));
            }
            if (obj.containsKey("weight")) {
                merchandises.setWeight(obj.getBigDecimal("weight"));
            }
            paramsMerchandises.add(merchandises);
        }
        parcel.setMerchandises(paramsMerchandises);
        parcels.add(parcel);
        target.setParcels(parcels);
        if (json.containsKey("warehouseCode")) {
            target.setWarehouseCode(json.getString("warehouseCode").trim());
        }
        if (json.containsKey("winitProductCode")) {
            target.setWinitProductCode(json.getString("winitProductCode").trim());
        }
        target.setValidationType("USABLE_P_ONLY");
        return target;
    }

    /**
     * 返利入参
     * 
     * @param json
     * @return OutboundOrderBillingVo
     */
    public static OutboundOrderRebateVo rebateOrderList(JSONObject json) {
        OutboundOrderRebateVo vo = new OutboundOrderRebateVo();
        if (json.containsKey("orderNo")) {// 订单号
            vo.setOrderNo(json.getString("orderNo"));
        }
        if (json.containsKey("sellerOrderNo")) {// 卖家订单号
            vo.setSellerOrderNo(json.getString("sellerOrderNo"));
        }
        if (json.containsKey("warehouseCode")) {// 仓库编码
            vo.setWarehouseCode(json.getString("warehouseCode"));
        }
        if (json.containsKey("sellerId")) {// 卖家id
            vo.setSellerId(json.getString("sellerId"));
        }
        if (json.containsKey("buyerId")) {// 买家id
            vo.setBuyerId(json.getString("buyerId"));
        }
        if (json.containsKey("eBayTransactionId")) {// eBay交易ID
            vo.setEbayTransactionId(json.getString("eBayTransactionId"));
        }
        if (json.containsKey("eBayItemId")) {// eBay商品ID
            vo.setEbayItemId(json.getString("eBayItemId"));
        }
        if (json.containsKey("updateItemId")) {// 出库更新eBay商品ID
            vo.setUpdateItemId(json.getString("updateItemId"));
        }
        if (json.containsKey("updateTransactionId")) {// 出库更新eBay交易ID
            vo.setUpdateTransactionId(json.getString("updateTransactionId"));
        }
        if (json.containsKey("buyerName")) {// 收件人姓名
            vo.setBuyerName(json.getString("buyerName"));
        }
        if (json.containsKey("orderDateFrom")) {// 开始时间
            vo.setOrderDateFrom(json.getDate("orderDateFrom"));
        }
        if (json.containsKey("orderDateFromTo")) {// 结束时间
            vo.setOrderDateFromTo(json.getDate("orderDateFromTo"));
        }
        if (json.containsKey("rebateStatus")) {// 返利状态
            vo.setRebateStatus(json.getString("rebateStatus"));
        }
        return vo;
    }

    /**
     * 构建返利列表记录
     * 
     * @param rebateOrderList
     * @return
     */
    public static List<Map<String, Object>> buildRebateOrderList(List<OutboundOrderRebateVo> rebateOrderList) {
        List<Map<String, Object>> resultlist = new ArrayList<Map<String, Object>>();
        for (OutboundOrderRebateVo rebateVo : rebateOrderList) {
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("orderNo", rebateVo.getOrderNo());
            resultMap.put("sellerOrderNo", rebateVo.getSellerOrderNo());
            resultMap.put("warehouseName", rebateVo.getWarehouseName());
            resultMap.put("productName", rebateVo.getWinitProductName());
            resultMap.put("buyerName", rebateVo.getBuyerName());
            resultMap.put("orderTime", rebateVo.getCreated());
            resultMap.put("rebateAmount", rebateVo.getRebateAmount());
            resultMap.put("rebateStatus", rebateVo.getRebateStatus());
            resultMap.put("remark", rebateVo.getRemark());
            resultMap.put("sellerId", rebateVo.getSellerId());
            resultMap.put("buyerId", rebateVo.getBuyerId());
            resultMap.put("ebayTransactionId", rebateVo.getEbayTransactionId());
            resultMap.put("ebayItemId", rebateVo.getEbayItemId());
            resultMap.put("updateTransactionId", rebateVo.getUpdateTransactionId());
            resultMap.put("updateItemId", rebateVo.getUpdateItemId());
            resultMap.put("lastUpdateDate", rebateVo.getLastUpdateDate());
            resultlist.add(resultMap);
        }
        return resultlist;
    }

    /**
     * 构建导出返利列表记录
     * 
     * @param rebateOrderList
     * @return
     */
    public static List<Map<String, Object>> buildExportRebateOrderList(List<OutboundOrderRebateVo> rebateOrderList) {
        List<Map<String, Object>> resultlist = new ArrayList<Map<String, Object>>();
        for (OutboundOrderRebateVo rebateVo : rebateOrderList) {
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("orderNo", rebateVo.getOrderNo());
            resultMap.put("sellerOrderNo", rebateVo.getSellerOrderNo());
            resultMap.put("warehouseName", rebateVo.getWarehouseName());
            resultMap.put("productName", rebateVo.getWinitProductName());
            resultMap.put("buyerName", rebateVo.getBuyerName());
            resultMap.put("orderTime", rebateVo.getCreated());
            resultMap.put("rebateAmount", rebateVo.getRebateAmount());
            resultMap.put("rebateStatus", rebateVo.getRebateStatus());
            resultMap.put("remark", rebateVo.getRemark());
            resultMap.put("sellerId", rebateVo.getSellerId());
            resultMap.put("buyerId", rebateVo.getBuyerId());
            resultMap.put("ebayTransactionId", rebateVo.getEbayTransactionId());
            resultMap.put("ebayItemId", rebateVo.getEbayItemId());
            resultlist.add(resultMap);
        }
        return resultlist;
    }

    /**
     * 构建批量导入返利
     * 
     * @param json
     * @return
     */
    public static OutboundOrderRebateVo buildRebateOrder(JSONObject jsons) {
        JsonValid json = new JsonValid(jsons);
        OutboundOrderRebateVo target = new OutboundOrderRebateVo();
        target.setOrderNo(json.getString("orderNo"));
        target.setSellerId(json.getString("sellerId"));
        target.setBuyerId(json.getString("buyerId"));
        target.setUpdateItemId(json.getString("ebayItemId"));
        target.setUpdateTransactionId(json.getString("ebayTransactionId"));
        return target;
    }

    public static void buildReturnOutboundOrderInfoByWorkOrder(Map<String, Object> map, OutboundOrderWorkOrderHelpVo vo) {
        // 工单模块信息
        map.put("orderNo", vo.getOrderNo() == null ? "" : vo.getOrderNo());// 订单编号
        map.put("status", vo.getStatus() == null ? "" : vo.getStatus());// 订单状态
        map.put("trackingNo", vo.getTrackingNo() == null ? "" : vo.getTrackingNo());// 挂号
        map.put("outWhTime", vo.getOutWhTime() == null ? "" : vo.getOutWhTime());// 出库时间
        map.put("isAllowClaim", vo.getIsAllowClaim() == null ? "" : vo.getIsAllowClaim()); // 是否支持索赔
        map.put("dispatchVendorCode", vo.getDispatchVendorCode() == null ? "" : vo.getDispatchVendorCode()); // 供应商编码
        map.put("dispatchVendorName", vo.getDispatchVendorName() == null ? "" : vo.getDispatchVendorName());// 供应商编码
        map.put("dispatchType", vo.getDispatchType() == null ? "" : vo.getDispatchType());// 派送方式
        map.put("whCurrencyCode", vo.getWhCurrencyCode() == null ? "" : vo.getWhCurrencyCode());// 仓库币种
        map.put("merchandiseQTY", vo.getMerchandiseQTY() == null ? "" : vo.getMerchandiseQTY());// 商品数量
        map.put("customerCode", vo.getCustomerCode() == null ? "" : vo.getCustomerCode());// 客户编码
        map.put("customerName", vo.getCustomerName() == null ? "" : vo.getCustomerName()); // 客户名称
        map.put("warehouseName", vo.getWarehouseName() == null ? "" : vo.getWarehouseName()); // 仓库名称
        map.put("distributorCode", vo.getDistributorCode() == null ? "" : vo.getDistributorCode());// 分销商客户编码
        map.put("distributorName", vo.getDistributorName() == null ? "" : vo.getDistributorName()); // 分销商客户名称
        map.put("deliveryAmount", vo.getDeliveryAmount() == null ? "" : vo.getDeliveryAmount());// 派送费
        map.put("vendorCode", vo.getVendorCode() == null ? "" : vo.getVendorCode());// 供应商编码
        map.put("vendorName", vo.getVendorName() == null ? "" : vo.getVendorName());// 供应商名称
        map.put("warehouseCode", vo.getWarehouseCode() == null ? "" : vo.getWarehouseCode());// 仓库编码
        map.put("productCode", vo.getProductCode() == null ? "" : vo.getProductCode());// 产品编码

    }

    public static void buildReturnOutboundOrderInfoByWorkOrder(Map<String, Object> map, OutboundOrderWorkOrderHelpVo2 vo) {
        // 工单模块信息
        map.put("orderNo", vo.getOrderNo() == null ? "" : vo.getOrderNo());// 订单编号
        map.put("trackingNo", vo.getTrackingNo() == null ? "" : vo.getTrackingNo());// 挂号
        map.put("status", vo.getStatus() == null ? "" : vo.getStatus());// 订单状态
        map.put("outWhTime", vo.getOutWhTime() == null ? "" : vo.getOutWhTime());// 出库时间
        map.put("isAllowClaim", "Y"); // 默认支持索赔
        map.put("dispatchVendorCode", vo.getDispatchVendorCode() == null ? "" : vo.getDispatchVendorCode()); // 供应商编码
        map.put("dispatchVendorName", vo.getDispatchVendorName() == null ? "" : vo.getDispatchVendorName());// 供应商编码
        map.put("dispatchType", vo.getDispatchType() == null ? "" : vo.getDispatchType());// 派送方式
        map.put("whCurrencyCode", vo.getWhCurrencyCode() == null ? "" : vo.getWhCurrencyCode());// 仓库币种
        map.put("merchandiseQTY", vo.getMerchandiseQTY() == null ? "" : vo.getMerchandiseQTY());// 商品数量
        map.put("customerCode", vo.getCustomerCode() == null ? "" : vo.getCustomerCode());// 客户编码
        map.put("customerName", vo.getCustomerName() == null ? "" : vo.getCustomerName()); // 客户名称
        map.put("warehouseName", vo.getWarehouseName() == null ? "" : vo.getWarehouseName()); // 仓库名称
        map.put("distributorCode", vo.getDistributorCode() == null ? "" : vo.getDistributorCode());// 分销商客户编码
        map.put("distributorName", vo.getDistributorName() == null ? "" : vo.getDistributorName()); // 分销商客户名称
        map.put("deliveryAmount", vo.getDeliveryAmount() == null ? "" : vo.getDeliveryAmount());// 派送费
        map.put("vendorCode", vo.getVendorCode() == null ? "" : vo.getVendorCode());// 供应商编码
        map.put("vendorName", vo.getVendorName() == null ? "" : vo.getVendorName());// 供应商名称
        map.put("warehouseCode", vo.getWarehouseCode() == null ? "" : vo.getWarehouseCode());// 仓库编码
    }
    
    public static void buildReturnOutboundOrderInfoByWorkOrder(List<Map<String, Object>> resultList,
                                                               OutboundOrderWorkOrderVo workOrderVo) {
        // 构建返回数据
        // 1.订单信息返回构建
        if (null != workOrderVo.getOrderHelpVos() && workOrderVo.getOrderHelpVos().size() > 0) {
            for (OutboundOrderWorkOrderHelpVo vo : workOrderVo.getOrderHelpVos()) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("orderNo", vo.getOrderNo() == null ? "" : vo.getOrderNo());// 订单编号
                map.put("status", vo.getStatus() == null ? "" : vo.getStatus());// 订单状态
                map.put("trackingNo", vo.getTrackingNo() == null ? "" : vo.getTrackingNo());// 挂号
                map.put("outWhTime", vo.getOutWhTime() == null ? "" : vo.getOutWhTime());// 出库时间
                map.put("isAllowClaim", vo.getIsAllowClaim() == null ? "" : vo.getIsAllowClaim()); // 是否支持索赔
                map.put("dispatchVendorCode", vo.getDispatchVendorCode() == null ? "" : vo.getDispatchVendorCode()); // 供应商编码
                map.put("dispatchVendorName", vo.getDispatchVendorName() == null ? "" : vo.getDispatchVendorName());// 供应商编码
                map.put("dispatchType", vo.getDispatchType() == null ? "" : vo.getDispatchType());// 派送方式
                map.put("whCurrencyCode", vo.getWhCurrencyCode() == null ? "" : vo.getWhCurrencyCode());// 仓库币种
                map.put("merchandiseQTY", vo.getMerchandiseQTY() == null ? "" : vo.getMerchandiseQTY());// 商品数量
                map.put("customerCode", vo.getCustomerCode() == null ? "" : vo.getCustomerCode());// 客户编码
                map.put("customerName", vo.getCustomerName() == null ? "" : vo.getCustomerName()); // 客户名称
                map.put("warehouseName", vo.getWarehouseName() == null ? "" : vo.getWarehouseName()); // 仓库名称
                map.put("distributorCode", vo.getDistributorCode() == null ? "" : vo.getDistributorCode());// 分销商客户编码
                map.put("distributorName", vo.getDistributorName() == null ? "" : vo.getDistributorName()); // 分销商客户名称
                map.put("deliveryAmount", vo.getDeliveryAmount() == null ? "" : vo.getDeliveryAmount());// 派送费
                map.put("vendorCode", vo.getVendorCode() == null ? "" : vo.getVendorCode());// 供应商编码
                map.put("vendorName", vo.getVendorName() == null ? "" : vo.getVendorName());// 供应商名称
                map.put("distributor", vo.getDispatchVendorName() == null ? "" : vo.getDispatchVendorName()); //派送商
                map.put("warehouseCode", vo.getWarehouseCode() == null ? "" : vo.getWarehouseCode());// 仓库编码
                map.put("orderType", vo.getOrderType() == null ? "" : vo.getOrderType()); // 订单类型
                map.put("consigneeName", vo.getConsigneeName() == null ? "" : vo.getConsigneeName());// 收货人名称
                map.put("consigneeEmail", vo.getConsigneeEmail() == null ? "" : vo.getConsigneeEmail());// 收货人Email
                map.put("consigneePhone", vo.getConsigneePhone() == null ? "" : vo.getConsigneePhone());// 收货人电话
                map.put("consigneeHouseNum", vo.getConsigneeHouseNum() == null ? "" : vo.getConsigneeHouseNum());// 收货人门牌号
                map.put("consigneeCountry", vo.getConsigneeCountry() == null ? "" : vo.getConsigneeCountry());// 收货人国家
                map.put("consigneeState", vo.getConsigneeState() == null ? "" : vo.getConsigneeState());// 收货人州
                map.put("consigneeCity", vo.getConsigneeCity() == null ? "" : vo.getConsigneeCity());// 收货人城市
                map.put("consigneeZipCode", vo.getConsigneeZipCode() == null ? "" :vo.getConsigneeZipCode());// 收货人邮编
                map.put("consigneeStreeOne",vo.getConsigneeStreeOne() == null ? "" : vo.getConsigneeStreeOne());// 收货人街道1
                map.put("consigneeStreeTwo", vo.getConsigneeStreeTwo() == null ? "" : vo.getConsigneeStreeTwo());// 收货人街道2
                map.put("slaDays", vo.getSlaDays() == 0 ? 0 : vo.getSlaDays());// 时效天数
                map.put("billNumber", vo.getBillNumber() == null ? "" : vo.getBillNumber());// 单据号（前端输入的原始数据）
                map.put("isTrackingService",StringUtils.isBlank(vo.getIsTrackingService()) ? "N" : vo.getIsTrackingService());// 是否有跟踪服务
                // 定义商品编码
                List<Map<String, Object>> merchandiseList = new ArrayList<Map<String,Object>>();
                if(null != vo.getMerchandises() && vo.getMerchandises().size() > 0){
                    for(OutboundOrderWorkOrderMerchandiseHelpVo merchandiseVo : vo.getMerchandises() ){
                        Map<String,Object> merchandiseMap = new HashMap<String, Object>();
                        merchandiseMap.put("goodsCode", merchandiseVo.getGoodsCode() == null ? "" : merchandiseVo.getGoodsCode());// 商品条码
                        merchandiseMap.put("goodsName", merchandiseVo.getGoodsName() == null ? "" : merchandiseVo.getGoodsName());// 商品英文名
                        merchandiseMap.put("merchandiseCode", merchandiseVo.getMerchandiseCode() == null ? "" : merchandiseVo.getMerchandiseCode());// 商品编码
                        if(null != merchandiseVo.getSingleCodes() && merchandiseVo.getSingleCodes().size() > 0){
                            List<String> itemList = new ArrayList<String>();
                            for (String itemSerno : merchandiseVo.getSingleCodes()) {
                                itemList.add(itemSerno);
                            }
                            merchandiseMap.put("singleNameList", itemList);
                        }
                        merchandiseList.add(merchandiseMap);
                    }
                    map.put("merchandiseList", merchandiseList);
                }
                resultList.add(map);
            }
        }
        // 2.错误信息返回构建
        if(null != workOrderVo.getErrorResults() && workOrderVo.getErrorResults().size() > 0){
            for(ErrorResult errorResult :workOrderVo.getErrorResults()){
                Map<String, Object> errorResultMap = new HashMap<String, Object>();
                errorResultMap.put("errorMsg", errorResult.getErrorMsg() == null ? "" : errorResult.getErrorMsg());// 错误信息
                errorResultMap.put("errorNo", errorResult.getErrorOrderNo() == null ? "" : errorResult.getErrorOrderNo());// 错误单号
                errorResultMap.put("errorCode", errorResult.getErrorCode() == null ? "" : errorResult.getErrorCode());// 错误代码
                resultList.add(errorResultMap);// 将错误信息返回
            }
        }

    }
    
    /**
     * 通过产品编码获得订单类型
     * 
     * @param winitProductCode 产品编码
     * @return
     */
    public static String getOrderType(String winitProductCode) {
        String orderType = null;
        if (winitProductCode.startsWith(OrderType.SD.getType())) {
            orderType = OrderType.SD.getType();
        } else if (winitProductCode.startsWith(OrderType.SP.getType())) {
            orderType = OrderType.SP.getType();
        } else if (winitProductCode.startsWith(OrderType.DS.getType())) {
            orderType = OrderType.DS.getType();
        }
        return orderType;
    }

    /**
     * @param ex
     */
    /**
     * 批量导入订单构建
     * 
     * @param json
     * @return
     */
    public static OutboundOrderVo bulidOldOrderVo(JSONObject jsons, String isEbayOrder) {
        JsonValid json = new JsonValid(jsons);

        OutboundOrderVo target = new OutboundOrderVo();
        target.setOrderSource(OrderSource.MI.getSource());
        target.setSaleType(OutboundOrderEnumContants.saleType.SI.getStatus());
        target.setBulkType(OutboundOrderEnumContants.bulkType.NLM.getStatus());
        // 是否重复订单
        String isRepeat = jsons.containsKey("duplicateOrder") ? jsons.getString("duplicateOrder") : null;
        target.setIsRepeat(StringUtils.isEmpty(isRepeat) ? "N" : isRepeat);
        // winit产品信息
        ProductInfo productInfo = new ProductInfo();
        productInfo.setWinitProductName(json.getString("shippingMethod"));
        target.setProductInfo(productInfo);
        // 仓库信息
        WarehouseInfo warehouseInfo = new WarehouseInfo();
        warehouseInfo.setWarehouseName(json.getString("warehouse"));
        target.setDispatchWarehouseInfo(warehouseInfo);

        BuyerInfo info = new BuyerInfo();
        // 收件人邮编
        info.setBuyerPostcode(json.getString("buyerPostcode"));
        // 收件人国家
        info.setBuyerCountry(json.getString("buyerCountry"));
        // 城市
        info.setBuyerCity(json.getString("buyerCity"));
        // 州、省
        info.setBuyerState(json.getString("buyerState"));
        // 收货人姓名
        info.setBuyerName(json.getString("buyerFullname"));
        // 收货人电话
        info.setBuyerPhone(json.getString("buyerPhoneNumber"));
        info.setBuyerAddress1(json.getString("buyerAddress1"));
        info.setBuyerAddress2(json.getString("buyerAddress2"));
        info.setBuyerEmail(json.getString("buyerEmail"));
        info.setBuyerHouseNo(json.getString("houseNo"));
        target.setBuyerInfoVo(info);

        // ebay信息
        EbayOrderVo ebayOrderVo = new EbayOrderVo();
        ebayOrderVo.setEbayOrderId(jsons.containsKey("eBayOrderID") ? jsons.getString("eBayOrderID") : null);
        ebayOrderVo.setEbayOrderNo(jsons.containsKey("eBayNo") ? jsons.getString("eBayNo") : null);
        ebayOrderVo.setIsEbay(isEbayOrder);
        ebayOrderVo.setEbayBuyerId(jsons.containsKey("eBayBuyerID") ? jsons.getString("eBayBuyerID") : null);
        ebayOrderVo.setEbaySellerId(jsons.containsKey("eBaySellerID") ? jsons.getString("eBaySellerID") : null);
        target.setEbayOrderVo(ebayOrderVo);

        // 商品信息
        List<OutboundOrderMerchandiseVo> merchandiseInfoList = new ArrayList<OutboundOrderMerchandiseVo>();
        JSONArray array = json.getJSONArray("products");
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            OutboundOrderMerchandiseVo merchandiseVo = new OutboundOrderMerchandiseVo();
            merchandiseVo.setEstimateDispatchQty(obj.getInteger("quantity"));
            merchandiseVo.setMerchandiseCode(obj.getString("skuNo"));
            merchandiseVo.setEbayItemId(obj.getString("itemNumber"));
            merchandiseVo.setEbayTransactionId(obj.getString("transactionID"));
            merchandiseVo.setSpecification(obj.getString("attribute"));
            merchandiseInfoList.add(merchandiseVo);
        }
        target.setMerchandiseInfoList(merchandiseInfoList);

        target.setSellerOrderNo(json.getString("sellerOrderNo"));
        target.setInsuranceType(json.getString("insuranceType"));

        return target;
    }

    /**
     * 【更新草稿订单】构建
     * 
     * @param json
     * @return
     */
    public static OutboundOrderVo buildUpdateDraftOrderVo(JSONObject jsons) {
        JsonValid json = new JsonValid(jsons);
        OutboundOrderVo target = new OutboundOrderVo();
        if (jsons.containsKey("winitProductCode")) {
            // 产品信息
            ProductInfo productInfo = new ProductInfo();
            String orderType = OutboundOrderfieldValidator.getOrderType(json.getString("winitProductCode"));
            productInfo.setWinitProductCategory3(orderType);
            productInfo.setWinitProductCode(json.getString("winitProductCode"));
            target.setProductInfo(productInfo);
        }
        // 收件人信息
        BuyerInfo info = new BuyerInfo();
        BuyerInfo originalBuyerInfo = new BuyerInfo();
        if (jsons.containsKey("buyerPostcode")) {
            // 收件人邮编
            info.setBuyerPostcode(json.getString("buyerPostcode"));
            originalBuyerInfo.setBuyerPostcode(json.getString("buyerPostcode"));
        }
        if (jsons.containsKey("buyerCountry")) {
            // 收件人国家
            info.setBuyerCountry(json.getString("buyerCountry"));
            originalBuyerInfo.setBuyerCountry(json.getString("buyerCountry"));
        }
        if (jsons.containsKey("buyerCity")) {
            // 城市
            info.setBuyerCity(json.getString("receiverCity"));
            originalBuyerInfo.setBuyerCity(json.getString("receiverCity"));
        }
        if (jsons.containsKey("buyerState")) {
            // 州、省
            info.setBuyerState(json.getString("buyerState"));
            originalBuyerInfo.setBuyerState(json.getString("buyerState"));
        }
        if (jsons.containsKey("buyerName")) {
            // 收货人姓名
            info.setBuyerName(json.getString("buyerName"));
            originalBuyerInfo.setBuyerName(json.getString("buyerName"));
        }
        if (jsons.containsKey("buyerPhone")) {
            info.setBuyerPhone(json.getString("buyerPhone"));
            originalBuyerInfo.setBuyerPhone(json.getString("buyerPhone"));
        }
        if (jsons.containsKey("buyerAddress1")) {
            info.setBuyerAddress1(json.getString("buyerAddress1"));
            originalBuyerInfo.setBuyerAddress1(json.getString("buyerAddress1"));
        }
        if (jsons.containsKey("buyerAddress2")) {
            info.setBuyerAddress2(json.getString("buyerAddress2"));
            originalBuyerInfo.setBuyerAddress2(json.getString("buyerAddress2"));
        }
        if (jsons.containsKey("buyerEmail")) {
            info.setBuyerEmail(json.getString("buyerEmail"));
            originalBuyerInfo.setBuyerEmail(json.getString("buyerEmail"));
        }
        if (jsons.containsKey("buyerHouseNo")) {
            info.setBuyerHouseNo(json.getString("buyerHouseNo"));
            originalBuyerInfo.setBuyerHouseNo(json.getString("buyerHouseNo"));
        }
        target.setBuyerInfoVo(info);
        target.setOriginalBuyerInfo(originalBuyerInfo);

        if (jsons.containsKey("orderNo")) {
            target.setOrderNo(json.getString("orderNo"));
        }
        if (jsons.containsKey("isRepeat")) {
            target.setIsRepeat(json.getString("isRepeat"));
        }
        if (jsons.containsKey("sellerOrderNo")) {
            target.setSellerOrderNo(json.getString("sellerOrderNo"));
        }
        return target;
    }

    public static ProductRecommendRequestParams buildValuationCalcDeliveryFeeTool(JSONObject json) {
        ProductRecommendRequestParams target = new ProductRecommendRequestParams();
        RequestParamsReceiveAddress address = new RequestParamsReceiveAddress();
        if (json.containsKey("country")) {
            address.setCountry(json.getString("country").trim());
        }
        if (json.containsKey("region")) {
            address.setState(json.getString("region").trim());
        }
        if (json.containsKey("city")) {
            address.setCity(json.getString("city").trim());
        }
        if (json.containsKey("zipCode")) {
            address.setPostCode(json.getString("zipCode").trim());
        }
       
        target.setAddress(address);
        
        Bpartner partner = CommandContext.getContext().get("apiBpartner");
        target.setCustomerCode(partner.getValue());
        
        JSONArray array = json.getJSONArray("productList");
        List<RequestParamsParcels>  parcels = new ArrayList<RequestParamsParcels>();
        RequestParamsParcels parcel = new RequestParamsParcels();
        List<RequestParamsMerchandises> paramsMerchandises = new ArrayList<RequestParamsMerchandises>();
        
        for (Object item : array) {
            RequestParamsMerchandises merchandises = new RequestParamsMerchandises(); 
            JSONObject obj = (JSONObject) item;
            if (obj.containsKey("productCode")) {
                merchandises.setSKU(obj.getString("productCode").trim());
            }
            if (null != obj.get("productNum") && StringUtils.isNotBlank(obj.get("productNum").toString())) {
                merchandises.setQty(obj.getInteger("productNum"));
            } else {
                merchandises.setQty(1);
            }
            if (obj.containsKey("specification")) {
                merchandises.setSpecification(obj.getString("specification").trim());
            }
            if (obj.containsKey("length")) {
                merchandises.setLength(obj.getBigDecimal("length"));
            }
            if (obj.containsKey("width")) {
                merchandises.setWidth(obj.getBigDecimal("width"));
            }
            if (obj.containsKey("height")) { 
                merchandises.setHeight(obj.getBigDecimal("height"));
            }
            if (obj.containsKey("weight")) {
                merchandises.setWeight(obj.getBigDecimal("weight"));
            }
            paramsMerchandises.add(merchandises);
        }
        parcel.setMerchandises(paramsMerchandises);
        parcels.add(parcel);
        target.setParcels(parcels);
        if (json.containsKey("warehouseCode")) {
            target.setWarehouseCode(json.getString("warehouseCode").trim());
        }
        if (!json.containsKey("winitProductCode") || StringUtils.isBlank(json.getString("winitProductCode"))) {
            target.setValidationType("USABLE_P_ONLY");
        } else{
            target.setWinitProductCode(json.getString("winitProductCode").trim());
            target.setValidationType("ESTIMATE");
        }
        if (json.containsKey("VASList")) {
            JSONArray vasArray = json.getJSONArray("VASList");
            List<RequestParamsVAS> vasList = new ArrayList<RequestParamsVAS>();
            for (Object vas : vasArray) {
                JSONObject obj = (JSONObject) vas;
                RequestParamsVAS vasParams = new RequestParamsVAS();
                vasParams.setCode(obj.getString("serviceCode"));
                JSONArray vasAttrArray = obj.getJSONArray("attrList");
                List<RequestParamsVASAttrs> paramAttrs = new ArrayList<RequestParamsVASAttrs>();
                for (Object attr : vasAttrArray) {
                    JSONObject attrObj = (JSONObject) attr;
                    RequestParamsVASAttrs paramAttr = new RequestParamsVASAttrs();
                    paramAttr.setAttrCode(attrObj.getString("attrCode"));
                    paramAttr.setAttrValue(attrObj.getString("attrValue"));
                    paramAttrs.add(paramAttr);
                }
                vasParams.setVasAttrs(paramAttrs);
                vasList.add(vasParams);
            }
            if (null != vasList && vasList.size() > 0) {
                target.setValueAddedServices(vasList);
            }
        }
        return target;
    }
    
    /**
     * 尾程派送费用计算器入参构建
     * @param json
     * @return
     */
    public static ProductRecommendRequestParams buildCalWHDeliveryParam(JSONObject json) {
        ProductRecommendRequestParams target = new ProductRecommendRequestParams();
        RequestParamsReceiveAddress address = new RequestParamsReceiveAddress();
        if (json.containsKey("country")) {
            address.setCountry(json.getString("country").trim());
        }
        if (json.containsKey("zipCode")) {
            address.setPostCode(json.getString("zipCode").trim());
        }
        if (json.containsKey("warehouseCode")) {
            target.setWarehouseCode(json.getString("warehouseCode").trim());
        }
        target.setAddress(address);
        
        Bpartner partner = CommandContext.getContext().get("apiBpartner");
        target.setCustomerCode(partner.getValue());
        
        List<RequestParamsParcels>  parcels = new ArrayList<RequestParamsParcels>();
        RequestParamsParcels parcel = new RequestParamsParcels();
        List<RequestParamsMerchandises> paramsMerchandises = new ArrayList<RequestParamsMerchandises>();
        
        JSONArray array = json.getJSONArray("merchandiseList");
        for (Object item : array) {
            RequestParamsMerchandises merchandises = new RequestParamsMerchandises(); 
            JSONObject obj = (JSONObject) item;
            if (obj.containsKey("merchandiseCode")) {
                merchandises.setSKU(obj.getString("merchandiseCode").trim());
            }
            if (null != obj.get("qty") && StringUtils.isNotBlank(obj.get("qty").toString())) {
                merchandises.setQty(obj.getInteger("qty"));
            } else {
                merchandises.setQty(1);
            }
            if (obj.containsKey("specification")) {
                merchandises.setSpecification(obj.getString("specification").trim());
            }
            if (obj.containsKey("length")) {
                merchandises.setLength(obj.getBigDecimal("length"));
            }
            if (obj.containsKey("width")) {
                merchandises.setWidth(obj.getBigDecimal("width"));
            }
            if (obj.containsKey("height")) { 
                merchandises.setHeight(obj.getBigDecimal("height"));
            }
            if (obj.containsKey("weight")) {
                merchandises.setWeight(obj.getBigDecimal("weight"));
            }
            paramsMerchandises.add(merchandises);
        }
        parcel.setMerchandises(paramsMerchandises);
        parcels.add(parcel);
        target.setParcels(parcels);
        target.setValidationType("USABLE_P_ONLY");
        return target;
    }
}
